home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6861 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  71 lines

  1. Path: news.eunet.ch!usenet
  2. From: Oliver Plohmann <opl@esec.ch>
  3. Newsgroups: comp.lang.c++
  4. Subject: Binary association problem
  5. Date: Tue, 20 Feb 1996 07:49:56 +0100
  6. Organization: ESEC SA
  7. Message-ID: <31296F14.88E@esec.ch>
  8. NNTP-Posting-Host: everest.esec.ch
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0b6a (X11; I; HP-UX A.09.05 9000/735)
  13.  
  14. Hello,
  15.  
  16. I habe two classes that reference each other. I put those classes into
  17. one header to get the code linked. Due to the nature of my application I
  18. have now several class associated with each eather in one header. My
  19. compiler can't handle it any more and creates a fatal error. I could
  20. switch compiler but every compiler has some problem. I think it would
  21. only mean substituting problems for other problems.
  22.  
  23. To split up the code into separate headers I have changed associations
  24. between classes to be pointer references (otherwise I get cyclic
  25. inclusion of headers). Whith this approach the code compiles and links
  26. fine. Below some sample code to show this. Unhappily, I get a protection
  27. violation. The reason is probably a scoping issue. Does anybody know a
  28. way out of this?
  29.  
  30. Thank you, Olli Plohmann
  31.  
  32. -------------------------a.h------------------------
  33.  
  34. class B;
  35.  
  36. class A {
  37.  
  38.         int* x;
  39. public:
  40.  
  41.         A() {};
  42.         ~A() {};
  43.  
  44.         void operator+(const int& i) {
  45.                 *x=i;   // protection violation !
  46.         };
  47. };
  48.  
  49. -------------------------b.h------------------------
  50. #include "a.h"
  51.  
  52. class A;
  53.  
  54. class B {
  55.  
  56.         A* x;
  57. public:
  58.  
  59.         B() { *x=5; };
  60.         ~B() {};
  61. };
  62.  
  63. --------------------------test.cpp--------------------
  64.  
  65. #include "b.h"
  66.  
  67. void main()
  68. {
  69.         B b;    // protection violation in A::operator=(const int& i)
  70. }
  71.